package com.model;

import com.bean.QuesAnsBean;
import com.jfinal.plugin.activerecord.Model;
import com.utils.ConstData;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Hexun on 2017/6/3 0003.
 */
public class Judge extends Model<Judge> {
    public static final Judge dao = new Judge();

    public void systemYuejuan(List<StudentPaper> studentPaper) {
        Judge judge=new Judge();
        Iterator it = studentPaper.iterator();
        while (it.hasNext()) {
            StudentPaper studentpaper = (StudentPaper) it.next();
            int ques_type = studentpaper.get("ques_type");
            int ques_id = studentpaper.get("ques_id");
            String answer = studentpaper.get("answer");
            QuesAnsBean qab = null;
            switch (ques_type) {
                case ConstData.CHOICE: {
                    qab = judge.load(new Integer(ques_id),
                            ConstData.CHOICE);
                    break;
                }
                case ConstData.CHOICEREAD: {
                    qab = judge.load(new Integer(ques_id),
                            ConstData.CHOICEREAD);
                    break;
                }
                case ConstData.SIMPLE_FILL: {
                    qab = judge.load(new Integer(ques_id),
                            ConstData.SIMPLE_FILL);
                    break;
                }
                case ConstData.COMPLEX_FILL: {
                    qab = judge.load(new Integer(ques_id),
                            ConstData.COMPLEX_FILL);
                    break;
                }
                case ConstData.JUDGE: {
                    qab = judge.load(new Integer(ques_id),
                            ConstData.JUDGE);
                    break;
                }
                case ConstData.SIMPlE_PRESS: {
                    qab = judge.load(new Integer(ques_id),
                            ConstData.SIMPlE_PRESS);
                    break;
                }
                case ConstData.PROGRAM: {
                    qab = judge.load(new Integer(ques_id),
                            ConstData.PROGRAM);
                    break;
                }
                case ConstData.GONGWENXIEZUO: {
                    qab = judge.load(new Integer(ques_id),
                            ConstData.GONGWENXIEZUO);
                    break;
                }
                // ==============================================================================
                case ConstData.CHOICE_FILL: {
                    qab = judge.load(new Integer(ques_id),
                            ConstData.CHOICE_FILL);
                    break;
                }
            }
            if (studentpaper.get("Answer")!= null){
                if (ConstData.SIMPlE_PRESS == ques_type||ConstData.GONGWENXIEZUO == ques_type||
                        ConstData.PROGRAM== ques_type){
                    // 这些类型的题目不作为
                }
                if (ConstData.COMPLEX_FILL == ques_type) {// 处理复杂填空题
                    String studentAnswer = "u" + answer.trim() + "u";
                    String standerAnswer = "u" + qab.getAnswer().trim() + "u";
                    String standerTem[] = standerAnswer.split("[@][*][*][@]");
                    String[] studentTem = studentAnswer.split("[@][*][*][@]");
                    if (studentTem.length == standerTem.length) {// 如果长度相等可以处理，若不相等则手工处理
                        int j = 0;
                        for (int i = 0; i < studentTem.length; i++) {
                            if (standerTem[i]
                                    .equalsIgnoreCase(studentTem[i])) {
                                j++;
                            }
                        }
                        studentpaper.set("isright", j);
                        if (j == standerTem.length) {// 判断是否全部正确
                            studentpaper.set("isMarked", ConstData.IS_MARKED);
                        }
                    } else {
                        studentpaper.set("isright", ConstData.WRONG);
                    }
                }
                if (ConstData.SIMPLE_FILL == ques_type) {
                    if (qab.getAnswer().trim().equalsIgnoreCase(answer.trim())) {
                        studentpaper.set("isright", ConstData.RIGHT);
                        studentpaper.set("isMarked", ConstData.IS_MARKED);
                    } else {
                        studentpaper.set("isright", ConstData.WRONG);
                    }
                }
                if (ConstData.CHOICEREAD == ques_type || ConstData.CHOICE == ques_type || ConstData.JUDGE == ques_type) {
                    if (qab.getAnswer().trim().equalsIgnoreCase(answer.trim())) {
                        studentpaper.set("isright", ConstData.RIGHT);
                        studentpaper.set("isMarked", ConstData.IS_MARKED);
                    } else {
                        studentpaper.set("isright", ConstData.WRONG);
                        studentpaper.set("isMarked", ConstData.IS_MARKED);
                    }
                }
                if (ConstData.CHOICE_FILL == ques_type) {
                    if (qab.getAnswer().trim().equalsIgnoreCase(answer.trim())) {
                        studentpaper.set("isright", ConstData.RIGHT);
                        studentpaper.set("isMarked", ConstData.IS_MARKED);
                    } else {
                        studentpaper.set("isright", ConstData.WRONG);
                        studentpaper.set("isMarked", ConstData.IS_MARKED);
                    }
                }
            } else {
                studentpaper.set("isright", ConstData.WRONG);
                studentpaper.set("isMarked", ConstData.IS_MARKED);
            }
            studentpaper.save();
        }
    }

    public QuesAnsBean load(Serializable id, int table) {
        try {
            QuesAnsBean qab = new QuesAnsBean();
            switch (table) {
                case ConstData.CHOICE: {
                    Choice c = Choice.dao.findById(id);
                    qab.setAnswer((String)c.get("answer"));
                    qab.setScore((String)c.get("Score"));
                    qab.setBody((String)c.get("body"));
                    break;
                }
                case ConstData.CHOICEREAD: {
                    ChoiceRead c = ChoiceRead.dao.findById(id);
                    qab.setAnswer((String)c.get("answer"));
                    qab.setScore((String)c.get("Score"));
                    qab.setBody((String)c.get("body"));
                    break;
                }
                case ConstData.SIMPLE_FILL: {
                    SimpleFill simplefill = SimpleFill.dao.findById(id);
                    qab.setAnswer((String)simplefill.get("answer"));
                    qab.setScore((String)simplefill.get("Score"));
                    qab.setBody((String)simplefill.get("body"));
                    break;
                }
                case ConstData.COMPLEX_FILL: {
                    ComplexFill cf = ComplexFill.dao.findById(id);
                    qab.setAnswer((String)cf.get("answer"));
                    qab.setScore((String)cf.get("Score"));
                    qab.setBody((String)cf.get("body"));
                    break;
                }
                case ConstData.JUDGE: {
                    Judge j = Judge.dao.findById(id);
                    qab.setAnswer((String)j.get("answer"));
                    qab.setScore((String)j.get("Score"));
                    qab.setBody((String)j.get("body"));
                    break;
                }
                case ConstData.SIMPlE_PRESS: {
                    SimplePress sp = SimplePress.dao.findById(id);
                    qab.setAnswer((String)sp.get("answer"));
                    qab.setScore((String)sp.get("Score"));
                    qab.setBody((String)sp.get("body"));
                    break;
                }
                // =====================================这里做过修改===================================================
                case ConstData.PROGRAM: {
                    Program sp = Program.dao.findById(id);
                    qab.setAnswer((String)sp.get("answer"));
                    qab.setScore((String)sp.get("Score"));
                    qab.setBody((String)sp.get("body"));
                    break;
                }
                case ConstData.GONGWENXIEZUO: {
                    Program sp = Program.dao.findById(id);
                    qab.setAnswer((String)sp.get("answer"));
                    qab.setScore((String)sp.get("Score"));
                    qab.setBody((String)sp.get("body"));
                    break;
                }
                // ==================================================================================================
                case ConstData.CHOICE_FILL: {
                    ChoiceFill c = ChoiceFill.dao.findById(id);
                    qab.setAnswer((String)c.get("answer"));
                    qab.setScore((String)c.get("Score"));
                    qab.setBody((String)c.get("body"));
                    break;
                }
            }
            return qab;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<StudentPaper> getStudentPaper(String sno, int pcId) {

        String sql = "select * from StudentPaper where sno = '" + sno
                + "' and paperconfig_id = " + pcId ;

        return StudentPaper.dao.find(sql);
    }

    public double getOneScore(String type, String question_id) {
        String sql = "select * from " + type + " where id = '"+question_id+"'";

        double onescore = 0;
        try {
            List<Score> scorelist= Score.dao.find(sql);
            onescore = Double.parseDouble((String)scorelist.get(0).get("Score"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return onescore;
    }

    public double getCFScore(String string, StudentPaper studentPaper) {

        String sql = "select * from " + string + " where id = "+studentPaper.get("ques_id");

        String[] answer1 = null;// 标准答案
        String[] answer2 = null;
        String[] scores = null;
        double onescore = 0;
        try {
            List<Score> scorelist= Score.dao.find(sql);

            scores = ((String)scorelist.get(0).get("Score")).split("&");
            answer1 =((String)scorelist.get(0).get("answer")).split("@[*][*]@");
            if (studentPaper.get("answer") == null
                    || "".equals(studentPaper.get("answer"))) {
                return 0;
            }
            answer2 = ((String)studentPaper.get("answer")).split("@[*][*]@");
            for (int i = 0; i < answer1.length; i++) {
                if (answer1[i].equalsIgnoreCase(answer2[i])) {
                    onescore += Double.parseDouble(scores[i]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return onescore;
    }

    public Score loadScore1(String sno, int courseId) {
        String sql="select * from Score sc ,(select * from student ) st , (select * from courses) co where sc.stuno=st.sno and sc.course=co.id";
        List<Score> scorel= Score.dao.find(sql);

        Iterator it = scorel.iterator();
        if (it.hasNext()) {
            Score s = (Score) it.next();
            return s;
        }
        return null;
    }
}
